// platformcoupon.js
var shihan = require('../../common/fetch')
var config = require('../../common/config')
var pages;
var currPage; //当前页面
var prevPage; //上一个页面
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      buytype: options.type || '',
      teamid: options.teamid || '',
      groupbuy: options.groupbuy || '',
      originbuy: options.originbuy || '',
      productid: options.productid || '',
      pronum: options.pronum || '',
      isoffline: options.isoffline || '',
      salesruleid: options.salesruleid || '',
      up_salesruleid: options.salesruleid || ''
    })
    this.pricechange()
    pages = getCurrentPages();
    currPage = pages[pages.length - 1]; //当前页面
    prevPage = pages[pages.length - 2]; //上一个页面
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    prevPage.setData({
      salesruleid: this.data.salesruleid
    })
  },
  selectcoupon: function(e) {
    var selectarr = [];
    var index = e.currentTarget.dataset.index;
    var thisparam = this.data.salesrulelist.sh_valid_salesrules;
    if (e.currentTarget.dataset.status == 'valid') {
      if (thisparam[index].sh_select_type == 0) {
        return
      }
      if (thisparam[index].selected) {
        thisparam[index].selected = false
      } else {
        thisparam[index].selected = true
      }
      this.setData({
        salesrulelist: this.data.salesrulelist
      })
      thisparam.forEach(function(el, index) {
        if (el.selected) {
          selectarr.push(el.sh_id)
        }
      })
      if (selectarr.length > 0) {
        this.setData({
          salesruleid: selectarr.join(',')
        })
        this.pricechangefn()
      } else {
        this.setData({
          salesruleid: ''
        })
        this.pricechangefn()
      }
    }
  },
  pricechange: function() {
    var data = {}
    var that = this
    if (that.data.isoffline) {
      data['is_offline'] = 1
    }
    data['address_id'] = that.data.addressid //收货地址id
    data['bupiao'] = that.data.bupiao //是否使用红包1为使用
    data['balance'] = that.data.balance //是否使用现金账户1为使用
    var salesruleid = wx.getStorageSync('salesruleidkey') || '';
    if (that.data.buytype == 'directbuy') {
      if (that.data.groupbuy == 1) {
        data['is_group_buy'] = 1
        data['team_id'] = that.data.teamid
      }
      if (that.data.originbuy == 1) {
        data["is_original_price"] = 1
      }
      data['buy_product'] = JSON.parse('{"' + that.data.productid + '"' + ':' + that.data.pronum + '}')
    } else {
      data['product_id'] = that.data.productid.split(',')
    }
    shihan.sendRequest(config.confirm_order, 'POST', data)
      .then(function(response) {
        var salesrules = response.data.data.sh_salesrule.sh_salesrules;
        if (salesrules.sh_valid_salesrules) {
          if (salesrules.sh_valid_salesrules.length > 0) {
            for (var i = 0; i < salesrules.sh_valid_salesrules.length; i++) {
              salesrules.sh_valid_salesrules[i].lookstatus = false
              salesrules.sh_valid_salesrules[i].selected = false
            }
          }
        }
        if (salesrules.sh_invalid_salesrules) {
          if (salesrules.sh_invalid_salesrules.length > 0) {
            for (var i = 0; i < salesrules.sh_invalid_salesrules.length; i++) {
              salesrules.sh_invalid_salesrules[i].lookstatus = false
            }
          }
        }
        if (that.data.salesruleid) {
          that.data.salesruleid.split(',').forEach(function(el, index) {
            response.data.data.sh_salesrule.sh_salesrules.sh_valid_salesrules.forEach(function(ele, index) {
              if (el == ele.sh_id) {
                ele.selected = true
              }
            })
          })
        }
        that.setData({
          salesrulelist: response.data.data.sh_salesrule.sh_salesrules
        })
        if (that.data.salesruleid) {
          data['salesrule_id'] = that.data.salesruleid
          that.pricechangefn()
        }
      }, function(error) {
        console.log(error);
      });
  },
  pricechangefn: function() {
    var data = {}
    var that = this
    if (that.data.isoffline) {
      data['is_offline'] = 1
    }
    data['address_id'] = that.data.addressid //收货地址id
    data['bupiao'] = that.data.bupiao //是否使用红包1为使用
    data['balance'] = that.data.balance //是否使用现金账户1为使用
    if (that.data.buytype == 'directbuy') {
      if (that.data.groupbuy == 1) {
        data['is_group_buy'] = 1
        data['team_id'] = that.data.teamid
      }
      if (that.data.originbuy == 1) {
        data["is_original_price"] = 1
      }
      data['buy_product'] = JSON.parse('{"' + that.data.productid + '"' + ':' + that.data.pronum + '}')
    } else {
      data['product_id'] = that.data.productid.split(',')
    }
    if (that.data.salesruleid) {
      data['salesrule_id'] = that.data.salesruleid
    }
    shihan.sendRequest(config.confirm_order_price, 'POST', data)
      .then(function(response) {
        that.data.salesrulelist.sh_valid_salesrules.forEach(function(el, index) {
          response.data.data.sh_salesrule.sh_valid_salesrules.forEach(function(ele, index) {
            if (el.sh_id == ele.sh_id) {
              el.sh_select_type = ele.sh_select_type
            }
          })
        })
        that.setData({
          salesrulelist: that.data.salesrulelist
        })
        console.log(that.data.salesrulelist)
      }, function(error) {
        console.log(error);
      });
  },
  confirmprice: function() {
    prevPage.setData({
      salesruleid: this.data.salesruleid
    })
    if (!this.data.salesruleid || (this.data.up_salesruleid != this.data.salesruleid)) {
      prevPage.setData({
        offlineselect: false
      })
    }
    wx.setStorage({
      key: "salesruleidkey",
      data: this.data.salesruleid,
      success: function() {
        wx.navigateBack()
      }
    });
  },
  closeerror: function() {
    clearInterval(time);
    var time;
    var that = this
    time = setTimeout(function() {
      that.setData({
        toast_flag: false
      })
    }, 1000)
  },
  lookinfo: function(e) {
    var index = e.currentTarget.dataset.index
    var saletype = e.currentTarget.dataset.type
    if (saletype == 'valid') {
      var couponitem = this.data.salesrulelist.sh_valid_salesrules
    } else if (saletype == 'invalid') {
      var couponitem = this.data.salesrulelist.sh_invalid_salesrules
    }

    if (couponitem[index].lookstatus == false) {
      couponitem[index].lookstatus = true
    } else {
      couponitem[index].lookstatus = false
    }
    this.setData({
      salesrulelist: this.data.salesrulelist,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  }
})